import { createStore } from 'vuex'

// 持久化数据
function storageFn(state) {
    localStorage.setItem('cartList', JSON.stringify(state.cartdatalist))
}
export default createStore({
  state: {
    cartdatalist:[]
  },
  getters: {
  },
  mutations: {
    // 添加商品
    addcart(state, payload) {
      const goods = state.cartdatalist.find((item) => item.id == payload.id);
      if (!goods) {
        state.cartdatalist.push({
          ...payload,
          acount: Number(payload.acount) ?? 1,
          checked: false,
        });
      } else {
        // 这是查到的重复数据，说明里面有相同数据,将其加1
        goods.acount += Number(payload.acount) ?? 1;
      }
      // 注意执行持久化函数
      storageFn(state);
    }
  },
  actions: {
  },
  modules: {
  }
})
